<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增角色' : '编辑角色'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="form.roleName" placeholder="请输入角色名称"/>
      </el-form-item>
      <el-form-item label="角色标识" prop="roleSign">
        <el-input v-model="form.roleSign" placeholder="如: admin"/>
      </el-form-item>
      <el-form-item label="角色备注" prop="remark">
        <el-input v-model="form.remark" placeholder="如:超级管理员"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/* eslint-disable */
import { save, update } from '@/api/role'
import Config from '@/config'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },

  },
  data() {
    return {
      loading: false,
      dialog: false,
      form: {},
      rules: {
        roleName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
            this.loading = true
            if (this.isAdd) {
              this.doAdd()
            } else {
              this.doEdit()
            }
        }
      })
    },
    doAdd() {
      save(this.form).then(res => {
        if (res.code == Config.code_200) {
          this.resetForm()
          this.$notify({ title: '添加成功', type: 'success' })
          this.$parent.initPageData()
        } else {
          this.$notify.error({ title: '失败', message: res.msg })
        }
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      update(this.form).then(res => {
        if (res.code == Config.code_200) {
          this.resetForm()
          this.$notify({ title: '修改成功', type: 'success' })
          this.$parent.initPageData()
        } else {
          this.$notify.error({ title: '失败', message: res.msg })
        }
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    },
  }
}
</script>

<style scoped>

</style>
